<template>
  <el-card>
    <h2>客户管理</h2>
    <el-button type="primary" @click="openAdd">新增客户</el-button>
    <el-table :data="customers" style="width: 100%;margin-top:16px">
      <el-table-column prop="customerId" label="ID" width="60" />
      <el-table-column prop="idNumber" label="证件号" />
      <el-table-column prop="fullName" label="姓名" width="100" />
      <el-table-column prop="contactPhone" label="联系电话" width="120" />
      <el-table-column prop="email" label="邮箱" />
      <el-table-column prop="address" label="地址" />
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button size="small" @click="openEdit(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="onDelete(scope.row.customerId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="600px">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
        <el-form-item label="证件号" prop="idNumber">
          <el-input v-model="form.idNumber" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="姓名" prop="fullName">
          <el-input v-model="form.fullName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" type="textarea" placeholder="请输入地址" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="onSubmit">确定</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { getCustomers, addCustomer, updateCustomer, deleteCustomer } from '../api/customer';
import { ElMessage, ElMessageBox } from 'element-plus';

const customers = ref([]);
const dialogVisible = ref(false);
const dialogTitle = ref('');
const form = ref({ 
  customerId: null, 
  idNumber: '', 
  fullName: '', 
  contactPhone: '', 
  email: '', 
  address: '' 
});
const formRef = ref();

const rules = {
  idNumber: [{ required: true, message: '请输入证件号', trigger: 'blur' }],
  fullName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  contactPhone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
  email: [
    { required: false, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ]
};

const loadCustomers = async () => {
  customers.value = await getCustomers();
};

const openAdd = () => {
  dialogTitle.value = '新增客户';
  form.value = { 
    customerId: null, 
    idNumber: '', 
    fullName: '', 
    contactPhone: '', 
    email: '', 
    address: '' 
  };
  dialogVisible.value = true;
};

const openEdit = (row) => {
  dialogTitle.value = '编辑客户';
  form.value = { ...row };
  dialogVisible.value = true;
};

const onSubmit = () => {
  formRef.value.validate(async (valid) => {
    if (!valid) return;
    if (!form.value.customerId) {
      await addCustomer(form.value);
      ElMessage.success('新增成功');
    } else {
      await updateCustomer(form.value);
      ElMessage.success('修改成功');
    }
    dialogVisible.value = false;
    loadCustomers();
  });
};

const onDelete = (id) => {
  ElMessageBox.confirm('确定要删除该客户吗？', '提示', { type: 'warning' })
    .then(async () => {
      await deleteCustomer(id);
      ElMessage.success('删除成功');
      loadCustomers();
    });
};

onMounted(loadCustomers);
</script> 